package com.course.server.service;


import com.course.server.domain.File;
import com.course.server.domain.FileExample;
import com.course.server.dto.FileDto;
import com.course.server.dto.PageDto;
import com.course.server.mapper.FileMapper;
import com.course.server.utils.CopyUtil;
import com.course.server.utils.UuidUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Date;
/**
 * 〈一句话功能简述>
 * 〈测试〉
 *
 * @Author metype
 * @Create 2021/5/26
 * @Since 1.0.0
 */
@Service
public class FileService {

    @Autowired
    private FileMapper fileMapper;

    public void list(PageDto pageDto) {
        PageHelper.startPage(pageDto.getPage(), pageDto.getSize());
        FileExample fileExample = new FileExample();
        List <File> fileList = fileMapper.selectByExample(fileExample);
        PageInfo <File> pageInfo = new PageInfo <>(fileList);
        pageDto.setTotal(pageInfo.getTotal());
        List <FileDto> fileDtoList = CopyUtil.copyList(fileList, FileDto.class);
        pageDto.setList(fileDtoList);
    }

    public void save(FileDto fileDto) {
        File file = CopyUtil.copy(fileDto, File.class);
        if (StringUtils.isEmpty(fileDto.getId())) {
            this.insert(file);
        } else {
            this.update(file);
        }
    }

    private void insert(File file) {
        Date now = new Date();
        file.setCreatedAt(now);
        file.setId(UuidUtil.getShortUuid());
        fileMapper.insert(file);
    }

    private void update(File file) {
        fileMapper.updateByPrimaryKeySelective(file);
    }

    public int del(String id) {
       return fileMapper.deleteByPrimaryKey(id);
    }

}